package ch.zbw.timeline.client.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ch.zbw.timeline.server.domain.Role;

public class ClientViewLogin extends JFrame {

	private JLabel lblUserName = new JLabel("Benutzername");
	private JLabel lblPassword = new JLabel("Passwort");
	private JLabel lblRole = new JLabel("Anmelden als");
	private JTextField txtUserName = new JTextField();
	private JPasswordField txtPassword = new JPasswordField();
	private JComboBox cmbRoles = new JComboBox();
	private JButton btnLogin = new JButton("Anmelden");
	
	private List<Role> roles;
	
	public ClientViewLogin(){
		init();		
	}
	
	private void init(){
		// Set position and size of the components
		lblUserName.setBounds(15, 15, 120, 25);
		lblPassword.setBounds(15, 45, 120, 25);
		lblRole.setBounds(15, 75, 120, 25);
		txtUserName.setBounds(130, 15, 200, 25);
		txtPassword.setBounds(130, 45, 200, 25);
		cmbRoles.setBounds(130, 75, 200, 25);
		btnLogin.setBounds(230, 110, 100, 25);
		
		// add components to window
		this.add(lblUserName);
		this.add(lblPassword);
		this.add(lblRole);
		this.add(txtUserName);
		this.add(txtPassword);
		this.add(cmbRoles);
		this.add(btnLogin);
		
		this.setTitle("timeLine - Anmeldung");
		this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		this.setSize(360, 180);
		this.setResizable(false);
		this.setLayout(null);
		this.setLocationRelativeTo(null);
	}
	
	public void addEventListener(final EventListener listener) {
		btnLogin.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				listener.loginButtonPressed();				
			}
		});
	}
	
	public void setRoles(List<Role> roles){
		this.roles = roles;
		
		for(Role role: roles){
			cmbRoles.addItem(role.getDescription());
		}
	}
	
	public String getUserName(){
		return txtUserName.getText();
	}
	
	public String getPassword(){
		return new String(txtPassword.getPassword());
	}
	
	public Role getRole(){
		int index = cmbRoles.getSelectedIndex();
		return roles.get(index);
	}

	
}
